package com.demo.forum.controller;

import com.demo.forum.common.Result;
import com.demo.forum.common.ResultCode;
import com.demo.forum.model.Board;
import com.demo.forum.model.exception.ApplicationException;
import com.demo.forum.services.impl.BoardServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
@RequestMapping("/borad")
@Api( tags = "板块接口")
@RestController
@Slf4j
public class BoradController {
    @Value("${forum.index.board-num : 9}")
    private Integer num;

    @Autowired
    private BoardServiceImpl boardService;

    @GetMapping("/topList")
    public  Result<List<Board>>  topList(){
        log.info("首页板块个数"+num);
        List<Board> boards = boardService.selectByNum(num);
        if(boards==null) {
            boards = new ArrayList<>();
        }
        return Result.success(boards);
    }

    @ApiOperation("获取板块信息")
    @GetMapping("/getById")
    public Result<Board> getById(@ApiParam("板块id") @RequestParam("id") @NonNull Long id){
        Board board = boardService.selectById(id);
        if(board == null || board.getDeleteState() == 1){
            log.error(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return Result.success(board);

    }
}
